package com.ssh.api.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.servlet.ServletContext;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.annotation.Scope;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;

@Scope("prototype")
@Results(value={@Result(name = Action.SUCCESS,type="stream",params={"inputName","inputStream"})})
public class DownloadAction extends ActionSupport implements ServletContextAware{
	private static final long serialVersionUID = 4564521218796883654L;
	private InputStream inputStream;
	private ServletContext servletContext;
	private String path;
	private String type="ai";
     


	public String execute()  {
		String separator=System.getProperty("file.separator");
		path=path.replaceFirst("_", "/");
      	String imagePath=this.servletContext.getInitParameter(type)+separator+path;
	  try {
		inputStream =  new FileInputStream(new File(imagePath));
	} catch (FileNotFoundException e) {
		 LogFactory.getDefaultLog().error("图片文件路径出错", LogName.API);
	}

	  return Action.SUCCESS;
	  }

	public InputStream getInputStream() {
	return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
	this.inputStream = inputStream;
	}

	@Override
	public void setServletContext(ServletContext arg0) {
		this.servletContext=arg0;
		
	}

	public void setPath(String path) {
		this.path = path;
	}
	  public void setType(String type) {
		this.type = type;
	}
}
